<%@ page contentType="text/html;charset=utf-8"%>
<%@ page
	import="java.io.*, java.sql.*, java.util.*, java.text.*, napal.*, util.*, napal.*"%>
<%@ page import="org.apache.ibatis.*"%>
<%@ page import="java.util.Date"%>
<script language="javascript">
function onPopup(r_id){
	var id =r_id; 
    popup = window.open("./RentalReason.jsp?r_id="+id, "RentalReason",'width=300,height=300,scrolling=no, scrollbars=no');
    popup.focus();
}
</script>
<%
	request.setCharacterEncoding("utf-8");
	String cmd = request.getParameter("cmd");
	String mRank = (String)session.getAttribute("managerRank");
	String m = (String)session.getAttribute("managerID");

 	if(m==null){ %>
	<script language=javascript>
				self.window.alert("권한이 없습니다.");
				location.href="Main.jsp?";
			</script>
<%}
 	

	if (request.getParameter("rentalDay") != null
			&& request.getParameter("rentalValue") != null) {
		if(mRank==null){ %> 
		<script language=javascript>
				self.window.alert("권한이 없습니다.");
				location.href="Main.jsp";
		</script>
	<%}
	else		
		if ("rental".equals(cmd)) {
					int equipmentID = Integer.parseInt(request
							.getParameter("select01"));
					String user_ID = request.getParameter("user_ID");
					int rentalDay = Integer.parseInt(request
							.getParameter("rentalDay"));
					
					int waitID = Integer.parseInt(request
							.getParameter("waitID"));
					String rentalValue = request.getParameter("rentalValue");
					String rentalmemo = request.getParameter("rentalmemo");
					RentalHome.insertRental(equipmentID, user_ID, rentalDay, rentalValue, rentalmemo);  
					RentalHome.EquipmentState(equipmentID);
					RentalHome.deleteWait(waitID); 
					if(RentalHome.getbook2(user_ID).isEmpty()==false)
						RentalHome.deletebook(user_ID);
					%> 
					<script language=javascript>
							self.window.alert("대여승인하였습니다.");
							location.href="RentalApp.jsp";
				</script>
					<%
				}
				else if("refuse".equals(cmd)){
					String user_ID = request.getParameter("user_ID");
					int waitID = Integer.parseInt(request
							.getParameter("waitID"));
					RentalHome.deleteWait(waitID); 
					if(RentalHome.getbook2(user_ID).isEmpty()==false)
						RentalHome.deletebook(user_ID);
					%> 
					<script language=javascript>
							self.window.alert("대여신청을 거절하였습니다.");
							location.href="RentalApp.jsp";
				</script>
					<%
				}
				
	
	}
	
%>

<%@ include file="./Header.jsp"%>
		</td>
		</TR>
		<TR HEIGHT="100">
		<td>
		<a href="RentalApp.jsp" class="btn btn-link btn-small">대여지급<br></a><br><br>
			<!--  <Table width="100%">
				<Tr>
					<td></td>
					<td><p align="right">
							<select name="category" class="span2">
								<input type="text" name="searchDB">
								<input name="cmd" type="submit" class="btn btn-inverse"
								value="search" placeholder="Search...">
						</p></td>
				</Tr>
			</Table>-->
			<table class="table" width="100%">
				<Tr>
					<td>카테고리명</td>
					<td>모델명</td>
					<td>보관사용처</td>
					<td>대여기간</td>
					<td>대여분류</td>
					<td>학과이름</td>
					<td>이름</td>
					<td>학번</td>
					<td>연락처</td>
					<td>대여사유</td>
					<td>기자재선택</td>
					<td>대여승인</td>
					
				</Tr>
				<%
					for (Return r : RentalHome.RentalEndApp()) {
				%>
				<TR>
					<form onsubmit="return confirm('해당 기자재를 대여승인/거절하시겠습니까?');">
						<td><%=r.get카테고리명()%></td>
						<td><%=r.get모델명()%></td>
						<td><%=r.get보관사용처() %></td>
						<td><%=r.get대여기간()%></td>
						<td><%=r.get대여분류()%></td>
						<td><%=r.get학과이름()%></td>
						<td><%=r.get이름()%></td>
						<Td><%=r.get학번()%></td>
						<td><%=r.get연락처()%></td>
						<td><a onClick="onPopup(<%=r.get대여대기번호() %>)" type="button"><font color="red">click</font></a></td>
						<td>
						
						
							<input type="hidden" name="waitID" value="<%= r.get대여대기번호()%>">
							<input type="hidden" name="user_ID" value="<%= r.get사용자ID()%>">
							<input type="hidden" name="rentalDay" value="<%= r.get대여기간()%>">
							<input type="hidden" name="rentalValue" value="<%= r.get대여분류()%>">
							<input type="hidden" name="rentalmemo" value="<%= r.get대여사유() %>">
						<select name="select01">
			<OPTION VALUE="0">===대여가능 기자재목록===</OPTION>

			<% for (Equipment e : RentalHome.getEquipmentID(r.get모델번호(), r.get보관사용처())) {%>
			<OPTION VALUE="<%=e.get기자재번호()%>"><%=e.get기자재관리번호()%></OPTION>

			<%} %>

		</SELECT>
		</td>
		<td>
		
							<input name="cmd" class="btn btn-inverse" type=submit
							value="rental">
							</td>
							 
						
					</form>
				</TR>
				<%
					}
				%>
			</table>
			
			
			
			
			
			</td>
		</tr>
		<TR HEIGHT="50">
			<TD><%@ include file="./bottom.jsp"%></TD>
		</TR>
	</TABLE>
</body>
</html>